from selenium import webdriver
# from common import *
import random
import time

def wlog(message):
    file=open(r"c\logs\green.txt","a",encoding="UFT-8")
    file.write(message+"\n")
    file.close()

def systime():
    return time.strftime("%Y-%m-%d %h:%M:%S",time.localtime)



def signup(name,password,email):
    firefox.find_element_by_xpath("ml/body/div[1]/div/div[2]/ul[4]/a").click()
    firefox.find_element_by_id("user_name").send_keys(name)
    firefox.find_element_by_id("user_password").send_keys(password)
    firefox.find_element_by_id("user_com_passwd").send_keys(password)
    firefox.find_element_by_id("user_email").send_keys(email)
    firefox.find_element_by_id("agreement").click()
    firefox.find_element_by_xpath("/tton[text()='提交注册用户']").click()
    try:
        if firefox.find_element_by_link_text("退出"):
            print("注册账号成功"+"\t"+systime())
            wlog("注册账号成功"+"\t"+systime())
    except:
        print("注册账号失败" + "\t" + systime())
        wlog("注册账号失败" + "\t" + systime())

def getstr(type=""):
    str1="abcdefghijklmnopqrstuvwxyz0123456789"
    #sample采样函数
    clist=random.sample(str1,random.sample(6,10))
    #join函数,将列表转换为字符串
    str2="".join(clist)
    if type=="email":
        return str2+"@qq.com"
    else:
        return str2


firefox = webdriver.Firefox()
firefox.get("http://localhost/dbshop")
for i in range(5):
    signup(getstr(),getstr(),getstr("email"))
firefox.quit()